package com.xzt.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RequestMapping("ai/client/")
@RestController
public class AIChatClientController {

    @Autowired
    private ChatClient chatClient;

    @GetMapping("/chat")
    public String generation(String userInput) {
        return this.chatClient.prompt()
                //用户输入的信息
                .user(userInput)
                //调用大模型
                .call()
                //返回文本
                .content();
    }

    record recipe(String recipeName, List<String> ingredients){}

    @GetMapping("/entity")
    public String entity(String userInput){
        recipe entity = this.chatClient
                .prompt()
                .user(String.format("请帮我生成%s的食谱", userInput))
                .call()
                .entity(recipe.class);
        return entity.toString();
    }

    @GetMapping(value = "/stream",produces = "text/html;charset=utf-8")
    public Flux<String> stream(String userInput){
        return chatClient.prompt()
                .user(userInput)
                .stream()
                .content();

    }

}
